package participation.Impl;

import cn.hutool.core.util.StrUtil;
import participation.dao.YzxInformationDao;
import participation.domain.YzxInformation;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class YzxInformationDaoImpl implements YzxInformationDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;

    @Override
    public void insert(YzxInformation yzxInformation) {
        String sql = "insert into yzx_information\n" +
                "        (id,number,typeid,name,begin,end,fund,leader,teacher,major_id) \n" +
                "values(?,?,?,?,?,?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,yzxInformation.getId());
            psmt.setString(2,yzxInformation.getNumber());
            psmt.setLong(3,yzxInformation.getTypeId());
            psmt.setString(4,yzxInformation.getName());
            psmt.setDate(5,yzxInformation.getBegin());
            psmt.setDate(6,yzxInformation.getEnd());
            psmt.setDouble(7,yzxInformation.getFund());
            psmt.setString(8,yzxInformation.getLeader());
            psmt.setString(9,yzxInformation.getTeacher());
            psmt.setLong(10,yzxInformation.getMajorId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long information_id) {
        String sql = "delete from yzx_information where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,information_id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(YzxInformation yzxInformation) {
        String sql = "update yzx_information set";
        List<Object> param = new ArrayList<>();
        if (yzxInformation == null) {
            return;
        } else {
            if (yzxInformation.getId() != null) {
                sql += " id = ?,";
                param.add(yzxInformation.getId());
            }
            if (yzxInformation.getNumber() != null) {
                sql += " number = ?,";
                param.add(yzxInformation.getNumber());
            }
            if (yzxInformation.getTypeId() != null) {
                sql += " typeId = ?,";
                param.add(yzxInformation.getTypeId());
            }
            if (yzxInformation.getName() != null) {
                sql += " name = ?,";
                param.add(yzxInformation.getName());
            }
            if (yzxInformation.getBegin() != null) {
                sql += " begin = ?,";
                param.add(yzxInformation.getBegin());
            }
            if (yzxInformation.getEnd() != null) {
                sql += " end = ?,";
                param.add(yzxInformation.getEnd());
            }
            if (yzxInformation.getFund() != null) {
                sql += " fund = ?,";
                param.add(yzxInformation.getFund());
            }
            if (yzxInformation.getLeader() != null) {
                sql += " leader = ?,";
                param.add(yzxInformation.getLeader());
            }
            if (yzxInformation.getTeacher() != null) {
                sql += " teacher = ?,";
                param.add(yzxInformation.getTeacher());
            }
            if (yzxInformation.getMajorId() != null) {
                sql += " major_id = ?,";
                param.add(yzxInformation.getMajorId());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,yzxInformation.getId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public YzxInformation selectById(Long id) {
        String sql = "select * from yzx_information where id = ?";
        YzxInformation yzxInformation = new YzxInformation();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                yzxInformation.setId(rs.getLong(1));
                yzxInformation.setNumber(rs.getString(2));
                yzxInformation.setTypeId(rs.getLong(3));
                yzxInformation.setName(rs.getString(4));
                yzxInformation.setBegin(rs.getDate(5));
                yzxInformation.setEnd(rs.getDate(6));
                yzxInformation.setFund(rs.getDouble(7));
                yzxInformation.setLeader(rs.getString(8));
                yzxInformation.setTeacher(rs.getString(9));
                yzxInformation.setMajorId(rs.getLong(10));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return yzxInformation;
    }

    @Override
    public List<YzxInformation> selectList(YzxInformation yzxInformation) {
        String sql = "select * from yzx_information where 1=1";
        List<YzxInformation> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (yzxInformation == null) {
            sql = "select * from yzx_information";
        } else {
            if (yzxInformation.getId() != null) {
                sql += " and id = ?";
                param.add(yzxInformation.getId());
            }
            if (yzxInformation.getNumber() != null) {
                sql += " and number = ?";
                param.add(yzxInformation.getNumber());
            }
            if (yzxInformation.getTypeId() != null) {
                sql += " and typeId = ?";
                param.add(yzxInformation.getTypeId());
            }
            if (yzxInformation.getName() != null) {
                sql += " and name = ?";
                param.add(yzxInformation.getName());
            }
            if (yzxInformation.getBegin() != null) {
                sql += " and begin = ?";
                param.add(yzxInformation.getBegin());
            }
            if (yzxInformation.getEnd() != null) {
                sql += " and end = ?";
                param.add(yzxInformation.getEnd());
            }
            if (yzxInformation.getFund() != null) {
                sql += " and fund = ?";
                param.add(yzxInformation.getFund());
            }
            if (yzxInformation.getLeader() != null) {
                sql += " and leader = ?";
                param.add(yzxInformation.getLeader());
            }
            if (yzxInformation.getTeacher() != null) {
                sql += " and teacher = ?";
                param.add(yzxInformation.getTeacher());
            }
            if (yzxInformation.getMajorId() != null) {
                sql += " and major_id = ?";
                param.add(yzxInformation.getMajorId());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                YzxInformation yzxInformation1 = new YzxInformation();
                yzxInformation1.setId(rs.getLong(1));
                yzxInformation1.setNumber(rs.getString(2));
                yzxInformation1.setTypeId(rs.getLong(3));
                yzxInformation1.setName(rs.getString(4));
                yzxInformation1.setBegin(rs.getDate(5));
                yzxInformation1.setEnd(rs.getDate(6));
                yzxInformation1.setFund(rs.getDouble(7));
                yzxInformation1.setLeader(rs.getString(8));
                yzxInformation1.setTeacher(rs.getString(9));
                yzxInformation1.setMajorId(rs.getLong(10));
                list.add(yzxInformation1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
